<template>
  <section>
    <!-- 面包屑 -->
 <breadcrumbs></breadcrumbs>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="按钮名称"
                    prop="name">
        <el-input v-model="searchForm.name"
                  placeholder="请输入按钮名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <btns @exec="exec"></btns>
    <div class="data-container scroll-table">
      <div>
        <div class="ind-title lh50 f16">按钮列表</div>
      </div>
      <!-- 按钮列表 -->
      <el-table :data="tableData"
                border
                style="width: 100%"
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="40">
        </el-table-column>
        <el-table-column prop="btnName"
                         label="按钮名称">
        </el-table-column>
        <el-table-column prop="btnId"
                         label="预览">
          <template slot-scope="scope">
            <el-button plain
                       size="small"
                       :type="scope.row.btnCode=='del'?'danger':''">{{scope.row.btnName}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="btnCode"
                         label="code">
        </el-table-column>
        <el-table-column prop="btnIcon"
                         label="icon">
        </el-table-column>
        <el-table-column prop="sort"
                         label="排序">
        </el-table-column>
        <el-table-column prop="status"
                         label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.status==0"
                     type="danger"
                     :underline="false">禁用</div>
            <div v-if="scope.row.status==1"
                     :underline="false">启用</div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--新增修改界面-->
    <el-dialog :title="funTit=='add'?'新增':'修改'"
               :visible.sync="addORUpdateVisible"
               width="500px"
               :close-on-click-modal="false">
      <el-form :model="btnForm"
               label-width="80px"
               :rules="rules"
               ref="btnForm">
        <el-form-item label="图标"
                      prop="btnIcon">
          <el-input v-model="btnForm.btnIcon"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="按钮名称"
                      prop="btnName">
          <el-input v-model="btnForm.btnName"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="方法code"
                      prop="btnCode">
          <el-input v-model="btnForm.btnCode"
                    auto-complete="off"
                    maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="排序"
                      prop="sort">
          <el-input v-model="btnForm.sort"
                    auto-complete="off"
                    @input="btnForm.sort=btnForm.sort.replace(/[^\d^\.]+/g,'')"
                    type="text"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click.native="addORUpdateVisible = false">取消</el-button>
        <el-button type="primary"
                   @click.native="submit('btnForm')">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

import btns from '@/components/btns.vue'
import breadcrumbs from "@/components/breadcrumbs.vue";
export default {
  components: {
    btns,
     breadcrumbs,
  },
  data () {
    return {
      searchForm: {
        name: ''
      },
      addORUpdateVisible: false,
      funTit: '',
      btnForm: {
        "btnIcon": "",
        "btnName": "",
        "btnCode": "",
        "sort": 0
      },
      rules: {
        "btnIcon": [],
        "btnName": [
          { required: true, message: '请输入按钮名称', trigger: 'blur' }
        ],
        "btnCode": [
          { required: true, message: '请输入方法code', trigger: 'blur' }
        ],
        "sort": [
          { required: true, message: '排序', trigger: 'blur' }
        ]
      },
      tableData: [],
      tableSelection: []
    }
  },
  created () {
    this.btnlist();
  },
  methods: {
    handleReset () {
      this.$refs.searchForm.resetFields();
      this.search()
    },
    search () {
      if (this.searchForm.name) {
        this.tableData = this.tableData.filter(e => e.btnName.indexOf(this.searchForm.name) >= 0);
      } else {
        this.btnlist();
      }
    },
    btnlist () {
      //获取接口
      this.$api.system.btnlist({}).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {

      this.btnForm = {
        "btnIcon": "",
        "btnName": "",
        "btnCode": "",
        "sort": 0
      }
      this.addORUpdateVisible = true;
    },
    update () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      this.btnForm = this.tableSelection[0];
      this.addORUpdateVisible = true;
    },
    del () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }

    },
    refresh () {
      this.btnlist();
    },
    enable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.status == 1) {
        this.$message.error('该数据已启用');
        return
      }
      this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.system.btnupdateStatus({ btnId: demo.btnId, status: 1 }).then(d => {
          if (d.code == 200) {
            this.btnlist();
            this.$message.success('启用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    disable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.status == 0) {
        this.$message.error('该数据已禁用');
        return
      }
      this.$confirm('此操作将禁用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.system.btnupdateStatus({ btnId: demo.btnId, status: 0 }).then(d => {
          if (d.code == 200) {
            this.btnlist();
            this.$message.success('禁用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.tableData.findIndex(c => c.btnCode == this.btnForm.btnCode) >= 0) {
            this.$message.error('code不能重复');
            return
          }
          if (this.funTit == 'add') {
            this.$api.system.btnadd(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            this.$api.system.btnupdate(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}

</script>